%%Onoma:Anastasios
%%Epwnumo:Vazakas
%%AEM:759
%%id:anvazaka

%% Or8ogwnioi pinakes


%%Askhsh 1:

a = [2 1 1]'
b = [1 2 -4]'
a'*b



%% Askhsh  2:

a = [2 1 1]';
 
b = [1 2 -4]';
 
c = cross(a,b);
 
figure(1); clf;
 
drawVector([a b c], 'r.', {'a','b','c'}); 

view(-160, 7)




%% Askhsh 3:

A = [a b c];

X = sampleUnitSphere(3,200);

AX = A*X;

figure(1); clf; hold on;

plot3( X(1,:), X(2,:), X(3,:),'b.'); view(-60,10)
plot3(AX(1,:),AX(2,:),AX(3,:),'g.');

drawVector(A, 'r.')

hold off; axis square equal; grid on;

norma = norm(A,2)


%% Askhsh 4:

B = [a b c];
C = diag([1/norm(a) 1/norm(b) 1/norm(c)]);
A = B*C;
X = sampleUnitSphere(3,200);
AX = A*X;
figure(1); clf; hold on; 
plot3( X(1,:), X(2,:), X(3,:),'b.'); 
view(-60,10) 
plot3(AX(1,:),AX(2,:),AX(3,:),'g.');

A*B;

ABX = A*B*X;

figure(2); clf; hold on;
 
plot3( X(1,:), X(2,:), X(3,:),'b.'); 

view(-60,10) 

plot3(ABX(1,:),ABX(2,:),ABX(3,:),'g.');


A*A*B;
AABX = A*A*B*X;
figure(3); clf; hold on; 
plot3( X(1,:), X(2,:), X(3,:),'b.'); 
view(-60,10) 
plot3(AABX(1,:),AABX(2,:),AABX(3,:),'g.');


A*A*A*B;
AAABX = A*A*A*B*X;
figure(4); clf; hold on; 
plot3( X(1,:), X(2,:), X(3,:),'b.'); 
view(-60,10) 
plot3(AAABX(1,:),AAABX(2,:),AAABX(3,:),'g.');

%% Askhsh 5:

A = [1 -1;1 1]
e1 = [1;0];e2 = [0;1];
figure(1); clf; 
hold on; 
drawVector([e1 e2], 'gs', {'e_1', 'e_2'}) 

a1 = [1 -1]
a2 = [1 1]'
a1*a2
drawVector(A, {'a','b'}); 
I = [1 0;0 1]
B = inv(A)*I

%% Ньсмет рйнЬкщн
%% Askhsh 1:

A = [-2 3; 
-3 1 ]; 
X = sampleUnitSphere(2, 100); 
AX = A*X;
figure(1); clf; hold on; 
title('ЕцбсмпгЮ фщн A B C рЬнщ уфпн мпнбдйбЯп кэклп.')
plot( X(1,:), X(2,:),'b.');
plot(AX(1,:),AX(2,:),'r.'); 

B = [1 3; 1 1]
X = sampleUnitSphere(2, 100); 
BX = B*X; 
plot(BX(1,:),BX(2,:),'r.'); 

C = reshape(1:4,2,2)
X = sampleUnitSphere(2, 100); 
CX = C*X;
plot(CX(1,:),CX(2,:),'r.'); 
hold off 
axis square equal

%% Askhsh 2:

B = [1 3; 1 1]
X = sampleUnitSphere(2, 100); 
BX = B*X;
figure(1); clf; hold on;
plot(X(1,:), X(2,:),'b.');
plot(BX(1,:),BX(2,:),'r.');

B1 = -B
B1X = B1*X;
plot(B1X(1,:),B1X(2,:),'r.');


B2 = B'
B2X = B2*X;
plot(B2X(1,:),B2X(2,:),'r.');
hold off 
axis square equal

%% Askhsh 3:

A = [-2 3; 
-3 1 ]; 
X = sampleUnitSphere(2, 100); 
AX = A*X;
figure(1); clf; hold on;
 
plot( X(1,:), X(2,:),'b.');

plot(AX(1,:),AX(2,:),'r.'); 

A = [-2 3; 
-3 1 ]; 

X = sampleUnitSphere(2, 100); 

AAX = A*A*X;

figure(2); clf; hold on; 

plot( X(1,:), X(2,:),'b.');

plot(AAX(1,:),AAX(2,:),'r.'); 

A = [-2 3; 
-3 1 ]; 

X = sampleUnitSphere(2, 100); 

AAAX = A*A*A*X;

figure(3); clf; hold on; 

plot( X(1,:), X(2,:),'b.');

plot(AAAX(1,:),AAAX(2,:),'r.'); 


A = [-2 3; 
-3 1 ]; 

X = sampleUnitSphere(2, 100); 

AAAAX = A*A*A*A*X;

figure(4); clf; hold on; 

plot( X(1,:), X(2,:),'b.');

plot(AAAAX(1,:),AAAAX(2,:),'r.'); 

A = [-2 3; -3 1 ]; 
X = sampleUnitSphere(2, 100); 
AAAAAX = A*A*A*A*A*X;
figure(5); clf; hold on; 
plot( X(1,:), X(2,:),'b.');
plot(AAAAAX(1,:),AAAAAX(2,:),'r.'); 

A = [-2 3; 
     -3 1 ]; 
X = sampleUnitSphere(2, 100); 
AAAAAAX = A*A*A*A*A*A*X;
figure(6); clf; hold on; 
plot( X(1,:), X(2,:),'b.');
plot(AAAAAAX(1,:),AAAAAAX(2,:),'r.'); 
hold off 
axis square equal

B = [1 3; 
     1 1];
X = sampleUnitSphere(2, 100); 
BX = B*X;
figure(1); clf; hold on;
plot(X(1,:), X(2,:),'b.');
plot(BX(1,:),BX(2,:),'r.');

B = [1 3; 
     1 1];
X = sampleUnitSphere(2, 100); 

BBX = B*B*X;

figure(2); clf; hold on;

plot(X(1,:), X(2,:),'b.');

plot(BBX(1,:),BBX(2,:),'r.');


B = [1 3; 
     1 1];

X = sampleUnitSphere(2, 100);
 
BBBX = B*B*B*X;

figure(3); clf; hold on;

plot(X(1,:), X(2,:),'b.');

plot(BBBX(1,:),BBBX(2,:),'r.');


B = [1 3; 
     1 1];

X = sampleUnitSphere(2, 100);
 
BBBBX = B*B*B*B*X;

figure(4); clf; hold on;

plot(X(1,:), X(2,:),'b.');

plot(BBBBX(1,:),BBBBX(2,:),'r.');


B = [1 3; 
     1 1]
X = sampleUnitSphere(2, 100); 
BBBBBX = B*B*B*B*B*X;
figure(5); clf; hold on;
plot(X(1,:), X(2,:),'b.');
plot(BBBBBX(1,:),BBBBBX(2,:),'r.');

B = [1 3; 1 1]
X = sampleUnitSphere(2, 100); 
BBBBBBX = B*B*B*B*B*B*X;
figure(6); clf; hold on;
plot(X(1,:), X(2,:),'b.');
plot(BBBBBBX(1,:),BBBBBBX(2,:),'r.');
hold off 
axis square equal

%% Askhsh 4:

A = [1 3 -2; 
     8 0 1; 
     2 2 6]
B = reshape(1:9,3,3)
X = sampleUnitSphere(3, 100);
AX = A*X;
figure(1); clf; hold on;
plot(X(1,:), X(2,:),'b.');
plot(AX(1,:),AX(2,:),'r.');

BX = B*X;
figure(2); clf; hold on;
plot(X(1,:), X(2,:),'b.');
plot(BX(1,:),BX(3,:),'r.');
rank(A)
rank(B)

%% Askhsh 5:

A = diag([1 2 3]);

norm(A,1)

%% Askhsh 6:

A = [1 3 -2; 8 0 1; 2 2 6];
B = reshape(1:9,3,3)

normaA = norm(A(:),2)
normaB = norm(B(:),2)
normaA*normaB

AB = A*B;
norma(AB) = norm(AB(:),2)

%% Provoles

%% Askhsh 1:

A = [1 0; 
     0 1; 
     1 0]

a = [1 2 3]'

figure(1); clf; hold on;

drawVector(a, 'r.', {'a'});

P_orth = A*A';

a_orth = P_orth *a;

drawVector([a_orth], 'b.', 'a_{orth}')

drawSpan(A)

hold off
 
view(150, 15)

B = [1 2; 
     0 1; 
     1 0]

figure(1); clf; hold on;

drawVector(a, 'r.', {'a'});

P_orth = B*B';

a_orth = P_orth *a;

drawVector([a_orth], 'b.', 'a_{orth}')

drawSpan(B)

hold off 

view(150, 15)

%% Askhsh 2:

A = [1 0; 
     0 1;  
     1 0]

a = [1 2 3]'

figure(1); clf; hold on;

drawVector(a, 'r.', {'a'});

P_oblique = A*A'*inv([A a]);

a_oblique = P_oblique*a;

drawVector([a_orth a_oblique], 'b.', 'a_{oblique}')

drawSpan(A)
 
hold off 

view(150, 15)


B = [1 2; 
     0 1; 
     1 0]

figure(1); clf; hold on;

drawVector(a, 'r.', {'a'});

P_oblique = B*B'*inv([B a]);

a_oblique = P_oblique*a;

drawVector([a_orth a_oblique], 'b.', 'a_{oblique}')

drawSpan(B) 

hold off 

view(150, 15)

%% Askhsh 3

I = eye(2);

q = [1 2]';

q = q/norm(q);

P = q*q';

P1 = I-P;

P2 = I-2*P;

a = [.8 .2]';

figure(1); clf; hold on;

drawVector(q,'r.', {'q'});

drawVector(a, 'gs', {'a'});

drawVector([P*a P1*a P2*a], {'Pa', '(I-P)a', '(I-2P)a'}, 'bs');
drawLine([a P*a]);

drawLine([a P2*a]);

hold off

%%  Askhsh 4

m = 5;

I = eye(m);

F = fliplr(I);

E = (I+F)/2;

if isequal(E*E,E), display('E is a projector.');end
if isequal(E',E)
    display('An orthogonal one.');
else
    display('But not orthogonal.');
end

%% Antistrofos pinakas

%% Askhsh 1:

[ 0.5;
 
  0.5 ]

[ -0.25;
   0.25 ]

%% Askhsh 2:

 A = [ 1 1; 
      -2 2 ];
e1 = [1;0]; e2 = [0;1];
figure(1); clf; hold on;
drawVector(A, {'a','b'});
drawVector([e1 e2], 'gs', {'e_1', 'e_2'})
invA = inv(A)
P1 = A*diag(invA(:,1));
P2 = A*diag(invA(:,2));
drawVector(P1, 'rd') 
drawVector(P2, 'ms')
drawLine([e1 P1(:,1)]);
drawLine([e1 P1(:,2)]);
drawLine([e2 P2(:,1)]);
drawLine([e2 P2(:,2)]);
hold off

%% Paragwntopoihsh LU

%% Askhsh 1:

A = [ 2 4; 
      3 6 ];
 
b = [4 7]';

x = A\b;

figure(1); clf; hold on;

drawVector(A, {'a_1', 'a_2'})

drawVector(b, 'rs', {'b'});

Pb = A*diag(x);

drawLine([b Pb(:,1)]);
drawLine([b Pb(:,2)]);
hold off;
title('Ax=b in the column space', 'FontSize', 14);

figure(2); clf; hold on;
drawPlane(A(1,:), b(1), 'b');
drawPlane(A(2,:), b(2), 'g');
hold off
title('Ax=b in the row space', 'FontSize', 14);

%% Askhsh 2:

A = [ 3 12 1; 
     12 0  2; 
      0 2  3 ]; 
b = [2.36 5.26 2.77]';
 
x = A\b;

Pb = A*diag(x);

figure(1);clf;hold on;

drawVector(A, {'a_1','a_2','a_3'});

drawVector(b, 'rs', {'b'});

drawLine([b Pb(:,1)]);

hold off;title('Ax=b in the column space', 'FontSize', 14);

figure(2); clf; hold on;

drawPlane(A(1,:), b(1), 'b'); 

drawPlane(A(2,:), b(2), 'g'); 

drawPlane(A(3,:), b(3), 'r');

plot3(x(1),x(2),x(3), 'mx', 'LineWidth', 4, 'MarkerSize', 15)

hold off

title('Ax=b in the row space', 'FontSize', 14);

%% Askhsh 3:

A = [ 4 1 ; 
      0 3.5 ]; 

b = [2 -3]';
 
x = A\b;

figure(1); clf; hold on;

drawPlane(A(1,:), b(1), 'b'); 

drawPlane(A(2,:), b(2), 'g');

hold off 

title('Ax=b in the row space', 'FontSize', 14); 

figure(1); hold on;

plot(x(1), x(2), 'rx', 'MarkerSize', 16, 'LineWidth', 2); 

hold off;

A = [ 4 1 ; 
      0 3.5 ]; 

b = [2 -3]';
 

x = A\b;

figure(2); clf; hold on; 

drawVector(A, {'a_1', 'a_2'})

drawVector(b, 'rs', {'b'}); 

Pb = A*diag(x);

drawVector(Pb, 'g+', {'x_{11}\cdot a_1', 'x_{21}\cdot a_2'});

drawLine([b Pb(:,1)]); 
drawLine([b Pb(:,2)]);
hold off;
title('Ax=b in the column space', 'FontSize', 14);

%% Askhsh 4:

U = [ 2 1 1 0; 
      0 1 1 1; 
      0 0 2 2; 
      0 0 0 2 ]; 

I=eye(4);

for j=1:4 
    b=I(1:4,j); 
    x(1:4,j)=backSub(U,b); 
end
V=U*x; 
display(x); 
display(V);

%% Askhsh 5:

A = [ 2 1 1 0; 
      4 3 3 1; 
      8 7 9 5; 
      6 7 9 8 ]; 

[L, U] = slu(A,1);

M = U\A

[M, A] = slu(U,1);

%% Askhsh 6:

A = [ 2 1 1 0; 
      4 3 3 1; 
      8 7 9 5; 
      6 7 9 8 ]; 

b = (2:2:8)'

[l u p] = lu(A)

p_b = p*b

y_lu = l\p_b

x_lu = u\y_lu

%% Askhsh 7:

A = [ 2 1 1 0; 
      4 3 3 1; 
      8 7 9 5; 
      6 7 9 8 ]; 

b = (2:2:8)'

[l u p] = lu(A)

p_b = p*b

y_lu = l\p_b

x_lu = u\y_lu


A = [ 2 1 1 0; 
      4 3 3 1; 
      8 7 9 5; 
      6 7 9 8 ];
 
[L,U] = slu(A,1);
